package cate.game.play.buff.h;

import cate.common.util.XT;
import cate.game.play.buff.BuffHandler;
import cate.game.play.buff.BuffParam;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.proce.action.ctx.ActionCtx;

import java.util.List;

/**
 * 添加时英雄进入灵能状态
 */
public class 英雄觉醒BH extends BuffHandler {

	public List<Integer> buffList;

	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		buffList = args.getIntList("buff");
	}

	@Override
	public void parseExt(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		buffList = XT.merge(buffList, args.getIntList("buff"));
	}

	//默认的觉醒回合数
	private static final int DEFAULT_AWAKE_ROUND = 3;

	@Override
	public void onAdd(ActionCtx action) {
		buff.owner.status.setAwaken(true);
		buff.owner.buff.forHandlers(h -> h.onAwaken(action,true));
		buff.owner.skill.forHandlers(h -> h.onAwaken(action,true));
		//附加buff的持续时间和原始buff的时间一致
		int buffLastRound = buff.cfg.getLastRound() > 0 ? buff.cfg.getLastRound() : DEFAULT_AWAKE_ROUND;
		BuffParam param = new BuffParam().exRoundSet(buffLastRound - DEFAULT_AWAKE_ROUND);
		if (XT.isNotBlank(buffList)) {
			for (Integer tid : buffList) {
				buff.owner.buff.tryAddByTid(action, buff.owner, tid, param);
			}
		}
	}

	@Override
	public void onInvalid(ActionCtx action, byte invalidReason) {
		buff.owner.status.setAwaken(false);
		buff.owner.buff.forHandlers(h -> h.onAwaken(action,false));
		buff.owner.skill.forHandlers(h -> h.onAwaken(action,false));
		if (XT.isNotBlank(buffList)) {
			for (Integer tid : buffList) {
				buff.owner.buff.removeBuffByTid(action, tid);
			}
		}
	}
}
